package com.gomokult.servlet.gamelib;

import com.gomokult.entity.GameDisplayInfo;
import com.gomokult.exception.InvalidIDException;
import com.gomokult.exception.NoSuchPageException;
import com.gomokult.service.impl.GamesLibraryServiceImpl;
import com.google.gson.Gson;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * 文件：DeleteCommentServlet.java
 * 作者：赵哲群
 * 完成度：已完成
 * 最后修改日期：2023-7-6
 */
@WebServlet("/servlet/QueryDisplayedGamesServlet")
public class QueryDisplayedGamesServlet extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        int userID = Integer.parseInt(request.getParameter("userID"));
        int page = Integer.parseInt(request.getParameter("page"));
        try {
            List<GameDisplayInfo> list = GamesLibraryServiceImpl.getSingleton().getUserDisplayedGamesFromPage(userID,page);
            Gson gson = new Gson();
            String json = gson.toJson(list);
            out.print(json);
        } catch (InvalidIDException e) {
            out.print("#InvalidIDException");
        } catch (NoSuchPageException e) {
            out.print("#NoSuchPageException");
        }

    }
}
